package system

import (
	"faker/project-public/common/response"
	"faker/project-public/global"
	"faker/project-public/model/sys"
	"faker/project-public/request"
	"faker/project-public/utils"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"time"
)

type SysRuleApi struct {
}

// CreateRule 创建rule
func (r *SysRuleApi) CreateRule(c *gin.Context) {
	var addRule request.AddRule
	err := c.ShouldBindJSON(&addRule)
	if err != nil {
		response.ErrorWithMessage(err.Error(), c)
		return
	}
	err = utils.Verify(addRule, utils.RuleVerify)
	if err != nil {
		response.ErrorWithMessage(err.Error(), c)
		return
	}
	ruleData := &sys.SysRule{Pid: addRule.Pid, Route: addRule.Rule, Icon: addRule.Icon, Rule: addRule.Route, Title: addRule.Title,
		IsMenu: addRule.IsMenu, SortId: addRule.SortId, Depth: addRule.Depth, CreateTime: int(time.Now().Unix())}
	err = ruleService.CreateRule(*ruleData)
	if err != nil {
		response.ErrorWithMessage(err.Error(), c)
		global.FAKER_LOG.Error("rule创建失败", zap.Error(err))
		return
	}
	response.SuccessWithMessage("创建成功", c)

}

// GetRuleInfo 获取rule信息
func (r *SysRuleApi) GetRuleInfo(c *gin.Context) {
	var idInfo request.GetById
	err := c.ShouldBindJSON(&idInfo)
	if err != nil {
		response.ErrorWithMessage(err.Error(), c)
		return
	}
	err = utils.Verify(idInfo, utils.IdVerify)
	if err != nil {
		response.ErrorWithMessage(err.Error(), c)
		return
	}
	RuleInfo, err := ruleService.GetRuleInfo(idInfo.ID)
	response.SuccessWithDetailed(gin.H{"rule_info": RuleInfo}, "查询成功", c)
}

// GetRuleList 获取rule列表
func (r *SysRuleApi) GetRuleList(c *gin.Context) {
	var pageInfo request.PageInfo
	err := c.ShouldBindJSON(&pageInfo)
	if err != nil {
		response.ErrorWithMessage(err.Error(), c)
		global.FAKER_LOG.Error(err.Error(), zap.Error(err))
		return
	}
	err = utils.Verify(pageInfo, utils.PageVerify)
	if err != nil {
		response.ErrorWithMessage(err.Error(), c)
		global.FAKER_LOG.Error(err.Error(), zap.Error(err))
		return
	}
	list, total, err := ruleService.GetMenuTree(pageInfo)
	response.SuccessWithDetailed(response.PageResult{
		List:     list,
		Total:    total,
		Page:     pageInfo.Page,
		PageSize: pageInfo.PageSize,
	}, "查询成功", c)
}

// SetRuleInfo 更新rule信息
func (r *SysRuleApi) SetRuleInfo(c *gin.Context) {
	var setRule sys.SysRule
	err := c.ShouldBindJSON(&setRule)
	if err != nil {
		response.ErrorWithMessage(err.Error(), c)
		global.FAKER_LOG.Error(err.Error(), zap.Error(err))
		return
	}
	err = utils.Verify(setRule, utils.RoleVerify)
	if err != nil {
		response.ErrorWithMessage(err.Error(), c)
		global.FAKER_LOG.Error(err.Error(), zap.Error(err))
		return
	}
	err = ruleService.SetRule(setRule)
	if err != nil {
		response.ErrorWithMessage(err.Error(), c)
		global.FAKER_LOG.Error("rule更新失败", zap.Error(err))
		return
	}
	response.SuccessWithMessage("更新成功", c)
}

// DeleteRule 删除角色
func (r *SysRuleApi) DeleteRule(c *gin.Context) {
	var idInfo request.GetById
	err := c.ShouldBindJSON(&idInfo)
	if err != nil {
		response.ErrorWithMessage(err.Error(), c)
		global.FAKER_LOG.Error(err.Error(), zap.Error(err))
		return
	}
	err = utils.Verify(idInfo, utils.IdVerify)
	if err != nil {
		response.ErrorWithMessage(err.Error(), c)
		return
	}
	err = ruleService.DeleteRule(idInfo.ID)
	if err != nil {
		response.ErrorWithMessage(err.Error(), c)
		global.FAKER_LOG.Error("删除失败", zap.Error(err))
		return
	}
	response.SuccessWithMessage("删除成功", c)
}

// GetAllRuleList 获取全部rule列表
func (r *SysRuleApi) GetAllRuleList(c *gin.Context) {
	list, err := ruleService.GetAllRules()
	if err != nil {
		response.ErrorWithMessage(err.Error(), c)
		global.FAKER_LOG.Error("获取失败", zap.Error(err))
		return
	}
	response.SuccessWithDetailed(gin.H{"rules": list}, "获取成功", c)
}
